home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Utilities / Calc / help / statement < prev    next >
Encoding:
Text File  |  1992-02-24  |  8.4 KB  |  228 lines  |  [TEXT/????]

  1. Statements
  2.  
  3.     Statements are very much like C statements.  Most statements act
  4.     identically to those in C, but there are minor differences and
  5.     some additions.  The following is a list of the statement types,
  6.     with explanation of the non-C statements.  In this list, upper
  7.     case words identify the keywords which are actually in lower case.
  8.     Statements are generally terminated with semicolons, except if the
  9.     statement is the compound one formed by matching braces.  Various
  10.     expressions are optional and may be omitted (as in RETURN).
  11.  
  12.  
  13.     NOTE: Calc commands are in lower case.   UPPER case is used below
  14.           for emphasis only, and should be considered in lower case.
  15.  
  16.  
  17.     IF (expr) statement
  18.     IF (expr) statement ELSE statement
  19.     FOR (optionalexpr ; optionalexpr ; optionalexpr) statement
  20.     WHILE (expr) statement
  21.     DO statement WHILE (expr)
  22.     CONTINUE
  23.     BREAK
  24.     GOTO label
  25.         These all work like in normal C.
  26.  
  27.     RETURN optionalexpr
  28.         This returns a value from a function.  Functions always
  29.         have a return value, even if this statement is not used.
  30.         If no return statement is executed, or if no expression
  31.         is specified in the return statement, then the return
  32.         value from the function is the null type.
  33.  
  34.     SWITCH (expr) { caseclauses }
  35.         Switch statements work similarly to C, except for the
  36.         following.  A switch can be done on any type of value,
  37.         and the case statements can be of any type of values.
  38.         The case statements can also be expressions calculated
  39.         at runtime.  The calculator compares the switch value
  40.         with each case statement in the order specified, and
  41.         selects the first case which matches.  The default case
  42.         is the exception, and only matches once all other cases
  43.         have been tested.
  44.  
  45.     { statements }
  46.         This is a normal list of statements, each one ended by
  47.         a semicolon.  Unlike the C language, no declarations are
  48.         permitted within an inner-level compound statement.
  49.         Declarations are only permitted at the beginning of a
  50.         function definition, or at the beginning of an expression
  51.         sequence.
  52.  
  53.     MAT variable [dimension] [dimension] ...
  54.     MAT variable [dimension, dimension, ...]
  55.  
  56.         This creates a matrix variable with the specified dimensions.
  57.         Matrices can have from 1 to 4 dimensions.  When specifying
  58.         multiple dimensions, you can use either the standard C syntax,
  59.         or else you can use commas for separating the dimensions.
  60.         For example, the following two statements are equivalent,
  61.         and so will create the same two dimensional matrix:
  62.  
  63.             mat foo[3][6];
  64.             mat foo[3,6];
  65.  
  66.         By default, each dimension is indexed starting at zero,
  67.         as in normal C, and contains the specified number of
  68.         elements.  However, this can be changed if a colon is
  69.         used to separate two values.  If this is done, then the
  70.         two values become the lower and upper bounds for indexing.
  71.         This is convenient, for example, to create matrices whose
  72.         first row and column begin at 1.  Examples of matrix
  73.         definitions are:
  74.  
  75.             mat x[3]    one dimension, bounds are 0-2
  76.             mat foo[4][5]    two dimensions, bounds are 0-3 and 0-4
  77.             mat a[-7:7]    one dimension, bounds are (-7)-7
  78.             mat s[1:9,1:9]    two dimensions, bounds are 1-9 and 1-9
  79.  
  80.         Note that the MAT statement is not a declaration, but is
  81.         executed at runtime.  Within a function, the specified
  82.         variable must already be defined, and is just converted to
  83.         a matrix of the specified size, and all elements are set
  84.         to the value of zero.  For convenience, at the top level
  85.         command level, the MAT command automatically defines a
  86.         global variable of the specified name if necessary.
  87.  
  88.         Since the MAT statement is executed, the bounds on the
  89.         matrix can be full expressions, and so matrices can be
  90.         dynamically allocated.  For example:
  91.  
  92.             size = 20;
  93.             mat data[size*2];
  94.  
  95.         allocates a matrix which can be indexed from 0 to 39.
  96.  
  97.     OBJ type { elementnames } optionalvariables
  98.     OBJ type variables
  99.  
  100.         These create a new object type, or create one or more
  101.         variables of the specified type.  For this calculator,
  102.         an object is just a structure which is implicitly acted
  103.         on by user defined routines.  The user defined routines
  104.         implement common operations for the object, such as plus
  105.         and minus, multiply and divide, comparison and printing.
  106.         The calculator will automatically call these routines in
  107.         order to perform many operations.
  108.     
  109.         To create an object type, the data elements used in
  110.         implementing the object are specified within a pair
  111.         of braces, separated with commas.  For example, to
  112.         define an object will will represent points in 3-space,
  113.         whose elements are the three coordinate values, the
  114.         following could be used:
  115.     
  116.             obj point {x, y, z};
  117.     
  118.         This defines an object type called point, whose elements
  119.         have the names x, y, and z.  The elements are accessed
  120.         similarly to structure element accesses, by using a period.
  121.         For example, given a variable 'v' which is a point object,
  122.         the three coordinates of the point can be referenced by:
  123.  
  124.             v.x
  125.             v.y
  126.             v.z
  127.  
  128.         A particular object type can only be defined once, and
  129.         is global throughout all functions.  However, different
  130.         object types can be used at the same time.
  131.  
  132.         In order to create variables of an object type, they
  133.         can either be named after the right brace of the object
  134.         creation statement, or else can be defined later with
  135.         another obj statement.  To create two points using the
  136.         second (and most common) method, the following is used:
  137.  
  138.             obj point p1, p2;    
  139.  
  140.         This statement is executed, and is not a declaration.
  141.         Thus within a function, the variables p1 and p2 must have
  142.         been previously defined, and are just changed to be the
  143.         new object type.  For convenience, at the top level command
  144.         level, object variables are automatically defined as being
  145.         global when necessary.
  146.  
  147.     EXIT string
  148.     QUIT string
  149.  
  150.         This command is used in two cases.  At the top command
  151.         line level, quit will exit from the calculator.  This
  152.         is the normal way to leave the calculator.  In any other
  153.         use, quit will abort the current calculation as if an
  154.         error had occurred.  If a string is given, then the string
  155.         is printed as the reason for quitting, otherwise a general
  156.         quit message is printed.  The routine name and line number
  157.         which executed the quit is also printed in either case.
  158.  
  159.         Quit is useful when a routine detects invalid arguments,
  160.         in order to stop a calculation cleanly.  For example,
  161.         for a square root routine, an error can be given if the
  162.         supplied parameter was a negative number, as in:
  163.  
  164.             define mysqrt(n)
  165.             {
  166.                 if (n < 0)
  167.                     quit "Negative argument";
  168.                 ...
  169.             }
  170.  
  171.         Exit is an alias for quit.
  172.  
  173.  
  174.     PRINT exprs
  175.  
  176.         For interactive expression evaluation, the values of all
  177.         typed-in expressions are automatically displayed to the
  178.         user.  However, within a function or loop, the printing of
  179.         results must be done explicitly.  This can be done using
  180.         the 'printf' or 'fprintf' functions, as in standard C, or
  181.         else by using the built-in 'print' statement.  The advantage
  182.         of the print statement is that a format string is not needed.
  183.         Instead, the given values are simply printed with zero or one
  184.         spaces between each value.
  185.  
  186.         Print accepts a list of expressions, separated either by
  187.         commas or colons.  Each expression is evaluated in order
  188.         and printed, with no other output, except for the following
  189.         special cases.  The comma which separates expressions prints
  190.         a single space, and a newline is printed after the last
  191.         expression unless the statement ends with a colon.  As
  192.         examples:
  193.  
  194.             print 3, 4;        prints "3 4" and newline.
  195.             print 5:;        prints "5" with no newline.
  196.             print 'a' : 'b' , 'c';    prints "ab c" and newline.
  197.             print;            prints a newline.
  198.  
  199.         For numeric values, the format of the number depends on the
  200.         current "mode" configuration parameter.  The initial mode
  201.         is to print real numbers, but it can be changed to other
  202.         modes such as exponential, decimal fractions, or hex.
  203.  
  204.         If a matrix or list is printed, then the elements contained
  205.         within the matrix or list will also be printed, up to the
  206.         maximum number specified by the "maxprint" configuration
  207.         parameter.  If an element is also a matrix or a list, then
  208.         their values are not recursively printed.  Objects are printed
  209.         using their user-defined routine.  Printing a file value
  210.         prints the name of the file that was opened.
  211.  
  212.  
  213.     SHOW item
  214.  
  215.         This command displays some information.
  216.         The following is a list of the various items:
  217.  
  218.             builtins    built in functions
  219.             globals        global variables
  220.             functions    user-defined functions
  221.             objfuncs    possible object functions
  222.             memory        memory usage
  223.     
  224.  
  225.     Also see the help topic:
  226.  
  227.         command         top level commands
  228.